﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RedSand.Web.Mvc;
using RedSand.Site.Models;

namespace RedSand.Site.Controllers
{
    public class NewsController : BaseController
    {
        public ActionResult Index()
        {
            return View(DropDownListHelper.GetNewsTypeList(false));
        }
        [AjaxOrChildAction]
        public ActionResult TopListNews(int CategoryID)
        {
            ViewBag.CategoryID = CategoryID;
            int Total = 0;
            List<NewsInfo> list = RsDataManager.GetList<NewsInfo>("NewsSearch", new {CategoryID = CategoryID, Status = 1 }, 1, 5, ref Total);
            return PartialView(list);
        }
        public ActionResult Search(int? Page, byte? CategoryId, string KeyWord, bool? notLayout)
        {
            notLayout = notLayout ?? false;
            ViewBag.Title = (RsDataManager.GetInfo<CategoryInfo>("CategoryGetInfo", new { CategoryId = CategoryId , Language = LanguageCode }) ?? new CategoryInfo()).Title;
            ViewBag.Type = (RsDataManager.GetInfo<CategoryInfo>("CategoryGetInfo", new { CategoryId = CategoryId, Language = LanguageCode }) ?? new CategoryInfo()).Type;
            ViewBag.TypeName = RedSand.Site.DropDownListHelper.GetNameTypeCategoryString((RsDataManager.GetInfo<CategoryInfo>("CategoryGetInfo", new { CategoryId = CategoryId , Language = LanguageCode }) ?? new CategoryInfo()).Type ?? 1);
            ViewBag.Title = string.IsNullOrWhiteSpace(KeyWord) ? ViewBag.Title : "Kết quả tìm kiếm với từ khóa '" + KeyWord + "\"";
            int Total = 0;
            ViewBag.PageSize = 10;
            List<NewsInfo> list = RsDataManager.GetList<NewsInfo>("NewsSearch", new { KeyWord = KeyWord, CategoryId = CategoryId, Status = 1, Language = LanguageCode }, Page ?? 1, ViewBag.PageSize, ref Total);
            ViewBag.TotalRecord = Total;
            ViewBag.RouteValues = new { CategoryId = CategoryId, KeyWord = KeyWord };
            if (notLayout.Value)
            {
                return PartialView("ListNews", list);
            }
            else
            {
                return View("ListNews", list);
            }
        }
        [AjaxOrChildAction]
        public ActionResult GetListPriority(int type,int TotalRecord)
        {
            @ViewBag.Title = RedSand.Site.DropDownListHelper.GetNameTypeCategoryString(type);
            List<NewsInfo> list = RsDataManager.GetList<NewsInfo>("NewsGetListPriorityByType", new { type = type, TotalRecord = TotalRecord, Language = LanguageCode });
            return PartialView(list);
        }

        
        public ActionResult GetListCategory(int type)
        {
            @ViewBag.Type = type;
            @ViewBag.Title = RedSand.Site.DropDownListHelper.GetNameTypeCategoryString(type);
            return View(RsDataManager.GetList<CategoryInfo>("CategorySearch", new { Type = type, Language = LanguageCode }));
        }
        public ActionResult Manager()
        {
            return View(new ManagerModel { AttachTinyMce = true, Title = Translate("NewsManager", "News") });
        }

        [AjaxOrChildAction]
        public ActionResult List(NewsSearchModel searchModel, int? page)
        {
            searchModel = searchModel ?? new NewsSearchModel();
            searchModel.Language = searchModel.Language ?? LanguageCode;
            searchModel.CategoryType = 1;
            int Total = 0;
            ViewBag.PageSize = 10;
            List<NewsInfo> list = RsDataManager.GetList<NewsInfo>("NewsSearch", searchModel, page ?? 1, ViewBag.PageSize, ref Total);
            ViewBag.TotalRecord = Total;
            searchModel.CategoryType = searchModel.CategoryType == 1 ? (byte?)null : 2;
            ViewBag.RouteValues = searchModel;
            return PartialView(list);
        }

        [AjaxOrChildAction]
        public ActionResult TopNew()
        {
            ViewBag.BoxTitle = "Tin tức mới";
            List<NewsInfo> list = RsDataManager.GetList<NewsInfo>("NewsGetTopNew", new { TotalRecord = 4 });
            return PartialView("TopNews", list);
        }

        [AjaxOrChildAction]
        public ActionResult TopHot()
        {
            ViewBag.BoxTitle = "Công trình nổi bật";
            List<NewsInfo> list = RsDataManager.GetList<NewsInfo>("NewsGetTopHot", new { TotalRecord = 4, Type = 3 });
            return PartialView("TopNews", list);
        }

        [AjaxOrChildAction]
        public ActionResult GetTopByType(int type, string title)
        {
            ViewBag.BoxTitle = title;
            ViewBag.type = type;
            List<NewsInfo> list = RsDataManager.GetList<NewsInfo>("NewsSearch", new { Type = type, Status = 1, PageSize = 4 });
            return PartialView("TopNews", list);
        }

        [AjaxOrChildAction]
        public ActionResult Activities()
        {
            List<NewsInfo> list = RsDataManager.GetList<NewsInfo>("NewsGetTopHot", new { TotalRecord = 4, Type = 2 });
            return PartialView(list);
        }

        [AjaxOrChildAction]
        public ActionResult Delete(int[] id)
        {
            if (id != null)
            {
                int count = id.Count();
                foreach (int i in id)
                {
                    NewsInfo info = RsDataManager.GetInfo<NewsInfo>("NewsGetInfo", "NewsID", i);
                    if (RsDataManager.Delete("NewsDelete", "NewsID", i))
                    {
                        DeleteFile(info.Image);
                        count--;
                    }
                }
                if (count > 0)
                {
                    AddMessage(string.Format(Translate("CannotDeleteAnyRecords", "Message"), count));
                }
                else
                {
                    AddMessage(Message.DeleteSuccess);
                }
            }
            return RedirectToAction("List");
        }

        [AjaxOrChildAction, HttpGet]
        public ActionResult Create()
        {
            return PartialView();
        }

        [AjaxOrChildAction, HttpPost]
        public ActionResult Create(NewsInfo info, string NewImage)
        {
            if (string.IsNullOrWhiteSpace(info.Description))
            {
                return AjaxMessage(Message.DescriptionEmpty);
            }
            if (string.IsNullOrWhiteSpace(info.Content))
            {
                return AjaxMessage(Message.ContentEmpty);
            }
            if (ModelState.IsValid)
            {
                info.Content = HtmlDecode(info.Content);
                info.Description = HtmlDecode(info.Description);
                info.Language = LanguageCode;
                if (!string.IsNullOrWhiteSpace(NewImage)) info.Image = SaveImage(null);
                info.CreateDate = DateTime.Now;
                info.Views = 0;
                info.UserID = UserID;
                info.PublishDate = info.PublishDate ?? info.CreateDate;
                if (RsDataManager.Save("NewsSave", info))
                {
                    AddMessage(Message.CreateSuccess);
                    return RedirectToAction("List");
                }
                else
                {
                    return AjaxMessage(Message.CreateError);
                }
            }
            return AjaxModelError();
        }

        [AjaxOrChildAction, HttpGet]
        public ActionResult Edit(int id)
        {
            return PartialView(RsDataManager.GetInfo<NewsInfo>("NewsGetInfo", "NewsID", id));
        }

        [AjaxOrChildAction, HttpPost]
        public ActionResult Edit(NewsInfo info, string NewImage)
        {
            if (string.IsNullOrWhiteSpace(info.Description))
            {
                return AjaxMessage(Message.DescriptionEmpty);
            }
            if (string.IsNullOrWhiteSpace(info.Content))
            {
                return AjaxMessage(Message.ContentEmpty);
            }
            if (ModelState.IsValid)
            {
                info.Content = HtmlDecode(info.Content);
                info.Description = HtmlDecode(info.Description);
                if (!string.IsNullOrWhiteSpace(NewImage)) info.Image = SaveImage(info.Image);

                if (RsDataManager.Save("NewsSave", info))
                {
                    AddMessage(Message.UpdateSuccess);
                    return RedirectToAction("List");
                }
                else
                {
                    return AjaxMessage(Message.UpdateError);
                }
            }
            return AjaxModelError();
        }

        [HttpGet]
        public ActionResult Details(int id)
        {
            var NewsInfo = RsDataManager.GetInfo<NewsInfo>("NewsGetInfo", "NewsID", id) ?? new NewsInfo();
            var UrlInfo = RsDataManager.GetInfo<CategoryInfo>("CategoryGetInfo", new { CategoryId = NewsInfo.CategoryID }) ?? new CategoryInfo();
            ViewBag.Type = UrlInfo.Type;
            ViewBag.TypeName = RedSand.Site.DropDownListHelper.GetNameTypeCategoryString(ViewBag.Type);
            ViewBag.CategoryName = UrlInfo.Title;
            ViewBag.Category = UrlInfo.CategoryID;
            //RsDataManager.ExecuteNonQuery("NewsAddViews", new { NewsID = id });
            return View(NewsInfo);
        }

        public ActionResult Related(int id, int CategoryID)
        {
            var list = RsDataManager.GetList<NewsInfo>("NewsSearch", new { CategoryID = CategoryID });
            if (list != null) list = list.Where(item => item.NewsID != id).ToList();
            return PartialView(list);
        }
    }
}
